#python -m data.api_data.api_userlist   文件直接执行代码
from .api_token import get_id,get_time,get_token
from util.time_util import timestran
import requests
import json
with open(r'data\json_data\userlist.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']


def get_userlist(username='',real_name=''):
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/roles/user/list'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'username': username, 'real_name': real_name, 'page': 1,' limit': 10}
    response = requests.post(url=url,headers=headers,json=json)
    data_list = response.json()['data']['list']
    for elm in data_list:
        for key in data['list']:
            if key == 'ctl_role':
                rolename = ''
                for role in elm[key]:
                    rolename = rolename+role['name']
                values.append(rolename)         
            elif key == 'data_role':
                rolelist = ''
                # values.append(elm[key])
                for v in  elm[key]:
                    rolelist = rolelist+v['name']
                    # print(v['name'])
                values.append(rolelist)    
            elif key == 'created_at':
                values.append(timestran(elm[key]))
            elif key == 'status':
                if elm[key] == 1:
                    values.append('已启用')
                else:
                    values.append('未启用')
            else:
                values.append(elm[key])
    return values
